package com.quanquan.user.controller.statisticsReport;

import base.BaseController;
import com.quanquan.user.entity.SelfHelpIntegral;
import com.quanquan.user.request.StatisticsReportRequestBody;
import com.quanquan.user.service.statisticsReport.SelfHelpIntegralStatisticsReportService;
import com.quanquan.user.service.statisticsReport.StoreConsuneRankingService;
import enums.ResCodeEnum;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import utils.CalendarUtil;
import utils.StringUtil;
import web.response.ResultBody;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * 功能描述:小票消费商铺排行
 *
 * @ClassName: StoreConsuneRankingController
 * @Author: xiaoqianh丶
 * @Date: 2019-05-28 09:44
 * @Version: V1.0
 */
@RestController
@RequestMapping("/store_consume_ranking")
public class StoreConsuneRankingController extends BaseController {
    public static final String DALITY = "daily";
    public static final String WEEK = "week";
    public static final String MONTH = "month";
    public static final String YEAR = "year";

    private final StoreConsuneRankingService storeConsuneRankingService;
    private final SelfHelpIntegralStatisticsReportService selfHelpIntegralStatisticsReportService;

    public StoreConsuneRankingController(SelfHelpIntegralStatisticsReportService selfHelpIntegralStatisticsReportService, StoreConsuneRankingService storeConsuneRankingService) {
        this.selfHelpIntegralStatisticsReportService = selfHelpIntegralStatisticsReportService;
        this.storeConsuneRankingService = storeConsuneRankingService;
    }

    /**
     * 每日小票消费总笔数,每周小票消费总笔数,每月小票消费总笔数
     *
     * @return
     */
    @PostMapping("/receipts_total")
    public ResultBody integralReceive(@RequestBody StatisticsReportRequestBody requestBody) {
        String statisticalCycle = requestBody.getStatisticalCycle();
        Date startTime = null;
        Date endTime = null;
        Map<String, Date> before = new HashMap<>();
        Map<String, Date> map = new HashMap<>();
        if (StringUtil.isNotEmpty(statisticalCycle)) {
            switch (statisticalCycle) {
                case DALITY:
                    before = CalendarUtil.setDayRange(map);
                    break;
                case WEEK:
                    //当前时间的本周开始时间和结束时间
                    before = CalendarUtil.setWeekRange(map);
                    break;
                case MONTH:
                    //当前时间的本月开始时间和结束时间
                    before = CalendarUtil.setMonthRange(map);
                    break;
                case YEAR:
                    CalendarUtil.setYearRange(map);
                    break;
                default:
                    throw new IllegalStateException("Unexpected value（参数错误）: " + statisticalCycle);
            }
        }
        startTime = before.get("startTime");
        endTime = before.get("stopTime");

        List<SelfHelpIntegral> selfHelpIntegrals = selfHelpIntegralStatisticsReportService.statisticalReportsReceive(startTime, endTime);
        Stream<SelfHelpIntegral> userStream = null;
        Map<String, Long> result = new HashMap<>();
        Map<String, Long> result2 = new HashMap<>();



        //  (每年按每月划分)
        result = selfHelpIntegrals.parallelStream().map(SelfHelpIntegral::getStoreId).collect(Collectors.groupingBy(Function.identity(), Collectors.counting()));
        //排序并限制个数
        result.entrySet().stream().sorted((o1, o2) -> o2.getValue().compareTo(o1.getValue())).limit(10).forEachOrdered(entry -> result2.put(entry.getKey(), entry.getValue()));
//        List<StoreVO> storeList = result.entrySet().stream().sorted(Comparator.comparing(Map.Entry::getValue))
//                .map(e -> new StoreVO(e.getKey(), e.getValue())).limit(10).collect(Collectors.toList());

        List<String> storeIds = result2.keySet().stream().collect(Collectors.toList());
        //将商户id替换上去

        return new ResultBody(ResCodeEnum.SUCCESS.getCode(),ResCodeEnum.SUCCESS.getMsg(),result);
    }




    /**
     * 小票积分前10商户排名 每日 每周 每月（笔数）
     * @return
     */
    @PostMapping("/number")
    public ResultBody storeDailyConsumeNum (@RequestBody StatisticsReportRequestBody requestBody){
        String statisticalCycle = requestBody.getStatisticalCycle();
        Date startTime = null;
        Date endTime = null;
        Map<String, Date> before = new HashMap<>();
        Map<String, Date> map = new HashMap<>();
        if (StringUtil.isNotEmpty(statisticalCycle)) {
            switch (statisticalCycle) {
                case DALITY:
                    before = CalendarUtil.setDayRange(map);
                    break;
                case WEEK:
                    //当前时间的本周开始时间和结束时间
                    before = CalendarUtil.setWeekRange(map);
                    break;
                case MONTH:
                    //当前时间的本月开始时间和结束时间
                    before = CalendarUtil.setMonthRange(map);
                    break;
                case YEAR:
                    CalendarUtil.setYearRange(map);
                    break;
                default:
                    throw new IllegalStateException("Unexpected value（参数错误）: " + statisticalCycle);
            }
        }
        startTime = before.get("startTime");
        endTime = before.get("stopTime");
        Map<String, Object> storeByNumber = storeConsuneRankingService.findStoreByNumber(startTime, endTime);
        return new ResultBody(ResCodeEnum.SUCCESS.getCode(),ResCodeEnum.SUCCESS.getMsg(),storeByNumber);
    }

    /**
     * 小票积分前10商户排名 每日 每周 每月（积分）
     * @return
     */
    @PostMapping("/integral")
    public ResultBody storeIntegral (@RequestBody StatisticsReportRequestBody requestBody){
        String statisticalCycle = requestBody.getStatisticalCycle();
        Date startTime = null;
        Date endTime = null;
        Map<String, Date> before = new HashMap<>();
        Map<String, Date> map = new HashMap<>();
        if (StringUtil.isNotEmpty(statisticalCycle)) {
            switch (statisticalCycle) {
                case DALITY:
                    before = CalendarUtil.setDayRange(map);
                    break;
                case WEEK:
                    //当前时间的本周开始时间和结束时间
                    before = CalendarUtil.setWeekRange(map);
                    break;
                case MONTH:
                    //当前时间的本月开始时间和结束时间
                    before = CalendarUtil.setMonthRange(map);
                    break;
                case YEAR:
                    CalendarUtil.setYearRange(map);
                    break;
                default:
                    throw new IllegalStateException("Unexpected value（参数错误）: " + statisticalCycle);
            }
        }
        startTime = before.get("startTime");
        endTime = before.get("stopTime");
        Map<String, Double> storeByNumber = storeConsuneRankingService.findStoreByIntegral(startTime, endTime);
        return new ResultBody(ResCodeEnum.SUCCESS.getCode(),ResCodeEnum.SUCCESS.getMsg(),storeByNumber);
    }

    /**
     * 小票积分前10商户排名 每日 每周 每月（金额）
     * @return
     */
    @PostMapping("/money")
    public ResultBody storeMoney (@RequestBody StatisticsReportRequestBody requestBody){
        String statisticalCycle = requestBody.getStatisticalCycle();
        Date startTime = null;
        Date endTime = null;
        Map<String, Date> before = new HashMap<>();
        Map<String, Date> map = new HashMap<>();
        if (StringUtil.isNotEmpty(statisticalCycle)) {
            switch (statisticalCycle) {
                case DALITY:
                    before = CalendarUtil.setDayRange(map);
                    break;
                case WEEK:
                    //当前时间的本周开始时间和结束时间
                    before = CalendarUtil.setWeekRange(map);
                    break;
                case MONTH:
                    //当前时间的本月开始时间和结束时间
                    before = CalendarUtil.setMonthRange(map);
                    break;
                case YEAR:
                    CalendarUtil.setYearRange(map);
                    break;
                default:
                    throw new IllegalStateException("Unexpected value（参数错误）: " + statisticalCycle);
            }
        }
        startTime = before.get("startTime");
        endTime = before.get("stopTime");
        Map<String, Double> storeByNumber = storeConsuneRankingService.findStoreByMoney(startTime, endTime);
        return new ResultBody(ResCodeEnum.SUCCESS.getCode(),ResCodeEnum.SUCCESS.getMsg(),storeByNumber);
    }

}